package yur.wedding.modules.sys.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import yur.wedding.common.annotation.SysLog;
import yur.wedding.common.utils.Constant;
import yur.wedding.modules.sys.entity.SysDeptEntity;
import yur.wedding.modules.sys.service.SysDeptService;

import yur.wedding.utils.PageUtils;
import yur.wedding.utils.R;
import yur.wedding.validator.ValidatorUtils;


/**
 * 部门管理
 *
 * @author Yur
 * @email ${email}
 * @date 2021-11-09 21:15:10
 */
@RestController
@RequestMapping("sys/sysdept")
public class SysDeptController  extends  AbstractController{
    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysdept:list")
    public List list(@RequestParam Map<String, Object> params) {

        List<SysDeptEntity> deptlist = sysDeptService.queryList(new HashMap<String, Object>());
        return deptlist;
    }

    /**
     * 单个的查询
     *
     * @return
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:sysdept:select")
    public R selectList() {

        List<SysDeptEntity> deptlist = sysDeptService.queryList(new HashMap<String, Object>());
       if (getUserId()== Constant.SUPER_ADMIN){
           SysDeptEntity deptEntity = new SysDeptEntity();
           deptEntity.setDeptId(0L);
           deptEntity.setName("Yur一级部门总部门");
           deptEntity.setParentId(-1L);
           deptEntity.setOpen(true);
           deptlist.add(deptEntity);
       }

        return R.ok().put("deptList",deptlist);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{deptId}")
    @RequiresPermissions("sys:sysdept:info")
    public R info(@PathVariable("deptId") Long deptId) {
        SysDeptEntity sysDept = sysDeptService.getById(deptId);

        return R.ok().put("menu", sysDept);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysdept:save")
    public R save(@RequestBody SysDeptEntity sysDept) {
        sysDeptService.save(sysDept);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysdept:update")
    public R update(@RequestBody SysDeptEntity sysDept) {
        ValidatorUtils.validateEntity(sysDept);
        sysDeptService.updateById(sysDept);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除部门")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysdept:delete")
    public R delete(Long deptId) {

      List<Long> Listdept=  sysDeptService.queryListParentId(deptId);
        if (Listdept.size()>0){
                return  R.error("请先删除下属部门");
        }

        sysDeptService.removeByIds(Arrays.asList(deptId));

        return R.ok();
    }

}
